﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FBDMaker
{
    class StringMaskParser
    {

        protected static List<string> listKey;

        static StringMaskParser()
        {
            string[] keys = { "tl", 
                                "st", "sn", 
                                "av", 
                                "nf1", "nl1", "nm2", 
                                "nf2", "nl2", "nm2", 
                                "nf3", "nl3", "nm3", 
                                "nf4", "nl4", "nm4", 
                                "nf5", "nl5", "nm5", 
                                "ed", "ed1", "ed2", "ed3", "ed4", "ed5",
                                "ct","ye"};
            listKey = new List<string>(keys);
        }

        public static void ParseWithDict(String Source, string Mask, Dictionary<string, string> Dict)
        {
            foreach (string s in Dict.Keys)
            {
                string s1 = "%" + s;
                string s2 = "(?<" + s.ToLower() + ">.+)";
                Mask=Mask.Replace(s1, s2);
            }
            Regex rx = new Regex(Mask,RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
            Match matches = rx.Match(Source);

            GroupCollection groups = matches.Groups;
            List <string> keys = Dict.Keys.ToList<string>();
            foreach(string s in keys)
            {
                if (groups[s].Success)
                    Dict[s] = groups[s].Value;
            }
               
           
        }
        public static Dictionary<string, string> Parse(String Source, string Mask  )
        {
            Dictionary<string, string> Dict=new Dictionary<string,string>();
            foreach (string s in listKey)
                Dict.Add(s, string.Empty);

            ParseWithDict(Source, Mask, Dict);
            return Dict;

        }
    }
}
